#include <stdlib.h>
#include <stdio.h>

//#define DEBUG 0

#include "../include/processor.h"

char *tabs_processor = "\t";

Processor *processor_create(Interrupt *interrupt, Memory *memory, Disco *disco, Console *console, Timer *timer)/*Interrupt i, Memory m, Console c, Timer t, Disco d*/
  {
	

/*
    hint = i;
    mem = m;
    con = c;
    tim = t;
    dis = d;*/
  
    Processor *processor = (Processor *) malloc(sizeof(Processor));	
    processor->pc = 0;
    processor-> i = interrupt;
    processor-> m = memory;
    processor-> d = disco;
    processor-> c = console;
    processor-> t = timer;
    
    processor -> k = kernel_create(processor, interrupt, memory, disco, console, timer); //add 5-12 CERTO???

    return processor;
    	
  }
  
void *run_processor(void *processor) //tnho acesso a m(memory), d(disco)
  {
    Processor p = * (Processor *) processor;

    int PC = p.pc; 

    while (1)
      {
//#ifdef DEBUG
printf("\n PC = %d", PC);
//#endif
      int RD = read_mem(PC,p.m);
      sleep(1); 
#ifdef DEBUG
printf("\n RD = %d\n", RD);
#endif
      
      printf("%sprocessor\n", tabs_processor);
	PC ++;


      // Check for Hardware Interrupt and if so call the kernel
      int thisInt = get_interrupt(p.i);
      if ( thisInt != 0)
        {
        // Call the kernel passing the interrupt number
        kernel.run(thisInt);
        // Kernel handled the last interrupt
        reset_interrupt(p.i, thisInt);
        }

      }

   }
